﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityStudentManagemnetSystem.BLL;
using UniversityStudentManagemnetSystem.DAO.DAL;

namespace UniversityStudentManagemnetSystem.UI
{
    public partial class StudentRegistrationPage : System.Web.UI.Page
    {
        Student aStudent = new Student();
        StudentEducationRegistration studentEducationRegistration = new StudentEducationRegistration();
        StudentRegistrationManager studentManager = new StudentRegistrationManager();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                Response.Redirect("~/Account/Login.aspx", false);
            }


            if (!IsPostBack)
            {
                PopulatedDepartmentDropDownList();
                PopulatedExamDropDownList();
                registerButton.Visible = false;
            }
        }


        private void PopulatedExamDropDownList()
        {
            ExamTypeManager manager = new ExamTypeManager();
            DataTable table = manager.GetAllExamType();
            examDropDownList.DataSource = table;
            examDropDownList.DataTextField = "Key";
            examDropDownList.DataValueField = "Value";
            examDropDownList.DataBind();

        }

        private void PopulatedDepartmentDropDownList()
        {
            DepartmentManager departmentManager = new DepartmentManager();
            DataTable departments = departmentManager.GetAllDepartments();
            departmentDropDownList.DataTextField = "Name";
            departmentDropDownList.DataValueField = "Id";
            departmentDropDownList.DataSource = departments;
            departmentDropDownList.DataBind();
        }


        protected void examDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        protected void certificateUploadButton_Click(object sender, EventArgs e)
        {

            GetCertificateFromPc();


        }


        private void GetCertificateFromPc()
        {
            studentEducationRegistration.CertificateName = certificateFileUpload.FileName;

            if (studentEducationRegistration.CertificateName == "")
            {
                fileUploadMessageLabel.Text = "Invalid filename supplied";
            }
            if (certificateFileUpload.PostedFile.ContentLength == 0)
            {
                fileUploadMessageLabel.Text = "Invalid file content";
            }
            studentEducationRegistration.CertificateName =
                System.IO.Path.GetFileName(studentEducationRegistration.CertificateName);
            if (studentEducationRegistration.FolderName == "")
            {
                fileUploadMessageLabel.Text = "Path not found";
            }
            try
            {
                if (certificateFileUpload.HasFile)
                {
                    string fileString = System.IO.Path.GetExtension(certificateFileUpload.FileName);
                    if (fileString.ToLower() == ".jpg" || fileString.ToLower() == ".bmp" || fileString.ToLower() == ".gif" ||
                        fileString.ToLower() == ".docx")
                    {
                        certificateFileUpload.SaveAs(@"E:\ASPNET\Certificate\" + certificateFileUpload.FileName);
                        fileUploadMessageLabel.Text = @"E:\ASPNET\Certificate" + certificateFileUpload.FileName;
                        successMessage.ForeColor = Color.DeepPink;
                        successMessage.Text = "File: " + certificateFileUpload.PostedFile.FileName +
                                                      " has been uploaded.";
                    }
                    else
                    {
                        fileUploadMessageLabel.Text = "Only image file can be uploaded. Try again please.";
                    }
                }
            }
            catch (Exception exObj)
            {
                fileUploadMessageLabel.Text = "Error: " + exObj.Message;
            }

        }


        protected void addToListButton_Click(object sender, EventArgs e)
        {
            try
            {

                DateTime regDate = DateTime.Today;
                aStudent.Name = studentNameTextBox.Text;
                aStudent.Email = emailTextBox.Text;
                aStudent.Address = addressTextBox.Text;
                aStudent.ContactNumber = contactNumberTextBox.Text;
                aStudent.Department.DeptId = Convert.ToInt32(departmentDropDownList.SelectedValue);
                if (dateTextBox.Text == "")
                {
                    dateTextBox.Text = regDate.ToString();
                }
                aStudent.RegistrationDate = Convert.ToDateTime(dateTextBox.Text);
                if (Session["Student"] == null)
                {
                    Session["Student"] = aStudent;
                }
                studentEducationRegistration.CgpaPercentage = (float)Convert.ToDouble(cgpaPercentageTextBox.Text);
                studentEducationRegistration.GradeLetterOthers = gradeLetterOthersTextBox.Text;
                studentEducationRegistration.Location = fileUploadMessageLabel.Text;
                studentEducationRegistration.CertificateName = successMessage.Text;
                studentEducationRegistration.ExamTitle = examDropDownList.SelectedValue;
                //Student Exam Education followed By Taimur  Sir Derection
                string message;
                string student;
                student = studentManager.AStudentEducationInformationSave(studentEducationRegistration, aStudent, out message);
                if (student != null)
                {
                    DataTable table = studentManager.GetCurrentStudentAllCertificate(aStudent);
                    successMessage.ForeColor = Color.BlueViolet;
                    registerButton.Visible = true;
                    successMessage.Text = message;
                    examGridView.DataSource = table;
                    examGridView.DataBind();
                }
            }

                catch(FormatException exception)
                {
                    successMessage.ForeColor = Color.Red;
                    successMessage.Text = exception.Message;

                }
            catch (Exception exception)
            {

                successMessage.ForeColor = Color.Red;
                successMessage.Text = exception.Message;

            }
            

           
        }

        protected void registerButton_Click(object sender, EventArgs e)
        {
            try
            {
                Student student = new Student();
                student = (Student)Session["Student"];
                string message = studentManager.Save(student);
                successMessage.ForeColor = Color.BlueViolet;
                successMessage.Text = message;
                Session.Clear();
                ClearTextBoxes();
                registerButton.Visible = false;

            }
                catch(FormatException exception)
                {
                    saveMessageLabel.ForeColor = Color.Red;
                    saveMessageLabel.Text = exception.Message;
                }
            catch (Exception ex)
            {

                saveMessageLabel.ForeColor = Color.Red;
                saveMessageLabel.Text = ex.Message;
            }

        }
        private void ClearTextBoxes()
        {
            studentNameTextBox.Text = string.Empty;
            emailTextBox.Text = string.Empty;
            cgpaPercentageTextBox.Text = string.Empty;
            contactNumberTextBox.Text = string.Empty;
            addressTextBox.Text = string.Empty;
            gradeLetterOthersTextBox.Text = string.Empty;

        }

    }
}